package com.kye.express.business.order.scan_code_addresses;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.TypedValue;
import android.view.View;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.business.order.scan_code_address.ScanCodeAddressEditActivity;
import com.kye.express.databinding.ActivityScanCodeAddressListBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.express.view.reclyerview.OnItemClickListener;
import com.kye.model.bean.order.scan_code_order.ScanCodeOrderAddress;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.MultiTypeAdapter;

public class ScanCodeAddressListActivity extends BaseAppCompatActivity implements ScanCodeAddressListContract.ScanCodeAddressListView {
    public static final String KEY_ADDRESS = "KEY_ADDRESS";

    public static final String KEY_ADDRESS_TYPE = "KEY_ADDRESS_TYPE";

    public static final String SEND_ADDRESS = "SEND_ADDRESS";
    public static final String RECEIVE_ADDRESS = "RECEIVE_ADDRESS";

    private static final int REQUEST_CODE_EDIT_ADDRESS = 1;

    private ActivityScanCodeAddressListBinding mBinding;
    private MultiTypeAdapter mAdapter;
    private AddressDataViewBinder mAddressDataViewBinder;
    private String mAddressType;
    private ScanCodeAddressListContract.ScanCodeAddressListPresenter mPresenter;
    private Dialog mDialog;

    public static void startActivityForResult(Activity activity, String addressType, int requestCode) {
        activity.startActivityForResult(
                new Intent(activity, ScanCodeAddressListActivity.class)
                        .putExtra(KEY_ADDRESS_TYPE, addressType),
                requestCode
        );
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_scan_code_address_list);

        initView();
        initEvent();
        initData();
    }

    private void initView() {
        mBinding.recyclerViewContent.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MultiTypeAdapter();
        mAdapter.setHasStableIds(true);
        mAddressDataViewBinder = new AddressDataViewBinder();
        mAdapter.register(ScanCodeOrderAddress.class, mAddressDataViewBinder);
        mBinding.recyclerViewContent.setAdapter(mAdapter);
        mBinding.recyclerViewContent.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .color(android.R.color.transparent)
                .size((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()))
                .build());
    }

    private void initEvent() {
        ToolbarManager.setToolbar(this, mBinding.toolbar);

        mAddressDataViewBinder.setOnItemClickListener(new OnItemClickListener<ScanCodeOrderAddress>() {
            @Override
            public void onClickItem(ScanCodeOrderAddress item, View view) {
                setResult(RESULT_OK, new Intent().putExtra(KEY_ADDRESS, item));
                finish();
            }
        });

        mAddressDataViewBinder.setOnEditClickListener(new OnItemClickListener<ScanCodeOrderAddress>() {
            @Override
            public void onClickItem(ScanCodeOrderAddress item, View view) {
                ScanCodeAddressEditActivity.startActivityForResult(ScanCodeAddressListActivity.this, item, mAddressType, REQUEST_CODE_EDIT_ADDRESS);
            }
        });

        mAddressDataViewBinder.setOnDeleteClickListener(new OnItemClickListener<ScanCodeOrderAddress>() {
            @Override
            public void onClickItem(ScanCodeOrderAddress item, View view) {
                mPresenter.deleteAddress(item.getGuid());
            }
        });

        RxView.clicks(mBinding.buttonAddAddress)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        ScanCodeAddressEditActivity.startActivityForResult(ScanCodeAddressListActivity.this, null, mAddressType, REQUEST_CODE_EDIT_ADDRESS);
                    }
                });
    }

    private void initData() {
        mAddressType = getIntent().getStringExtra(KEY_ADDRESS_TYPE);
        mPresenter = new ScanCodeAddressListContract.ScanCodeAddressListPresenter();
        mPresenter.bind(this);
        mPresenter.setAddressType(mAddressType);
        mPresenter.getAddressList();
    }

    @Override
    public void onError() {
        ToastManager.show(this, R.string.common_net_error);
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg);
    }

    @Override
    public void onLoading() {
        mDialog = KyeProgressDialog.show(this, mDialog);
    }

    @Override
    public void onLoaded() {
        KyeProgressDialog.dismiss(mDialog);
    }

    @Override
    public void onAddressList(List<ScanCodeOrderAddress> addressData) {
        mAdapter.setItems(addressData);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_EDIT_ADDRESS && resultCode == RESULT_OK && data != null) {
            mPresenter.getAddressList();
        }
    }
}
